﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Gedea.Model.Entities;
using Gedea.Model;
using System.Reflection;
using Gedea.Core.Manager;
using Gedea.Core.Manager.DataAccess;

namespace Gedea.Core.Repositories
{
    public class UserRepository
    {
        public void Add(User user)
        {
            using (SqlConnection conn = new SqlConnection(DataConnection.GetConnection()))
            {
                string statement =
                    string.Format("INSERT INTO Users ({0}, {1}, {2}, {3}, {4}, {5}, {6}) VALUES (?, ?, ?, ?, ?, ?, ?)"
                    , DBObjects.Users.name.ToString()
                    , DBObjects.Users.nameForBank.ToString()
                    , DBObjects.Users.cuit.ToString()
                    , DBObjects.Users.benefit.ToString()
                    , DBObjects.Users.multibank.ToString()
                    , DBObjects.Users.largoReferenciaUnivDelDev.ToString()
                    , DBObjects.Users.recordsDuration.ToString());

                SqlCommand command = new SqlCommand(statement, conn);

                command.Parameters.Add(DBObjects.Users.name.ToString(), SqlDbType.VarChar).Value = user.Name;
                command.Parameters.Add(DBObjects.Users.nameForBank.ToString(), SqlDbType.VarChar).Value = user.NameForBank;
                command.Parameters.Add(DBObjects.Users.cuit.ToString(), SqlDbType.VarChar).Value = user.Cuit;
                command.Parameters.Add(DBObjects.Users.benefit.ToString(), SqlDbType.VarChar).Value = user.Benefit;
                command.Parameters.Add(DBObjects.Users.multibank.ToString(), SqlDbType.Bit).Value = false;
                command.Parameters.Add(DBObjects.Users.largoReferenciaUnivDelDev.ToString(), SqlDbType.Int).Value = 8;
                command.Parameters.Add(DBObjects.Users.recordsDuration.ToString(), SqlDbType.Int).Value = 2;

                DataConnection.Open(conn);

                command.ExecuteNonQuery();
            }
        }

        public User Get()
        {
            return new DataLookFor().Get<User>();
        }

        public void Update(User user)
        {
            using (SqlConnection conn = new SqlConnection(DataConnection.GetConnection()))
            {
                string statement =
                    string.Format("UPDATE Users SET {0} = ?, {1} = ?, {2} = ?, {3} = ?, {4} = ?, {5} = ?, {6} = ? WHERE {7} = ?;"
                    , DBObjects.Users.name.ToString()
                    , DBObjects.Users.nameForBank.ToString()
                    , DBObjects.Users.cuit.ToString()
                    , DBObjects.Users.benefit.ToString()
                    , DBObjects.Users.multibank.ToString()
                    , DBObjects.Users.largoReferenciaUnivDelDev.ToString()
                    , DBObjects.Users.recordsDuration.ToString()
                    , DBObjects.Users.userId.ToString());

                SqlCommand command = new SqlCommand(statement, conn);

                command.Parameters.Add(DBObjects.Users.name.ToString(), SqlDbType.VarChar).Value = user.Name;
                command.Parameters.Add(DBObjects.Users.nameForBank.ToString(), SqlDbType.VarChar).Value = user.NameForBank;
                command.Parameters.Add(DBObjects.Users.cuit.ToString(), SqlDbType.VarChar).Value = user.Cuit;
                command.Parameters.Add(DBObjects.Users.benefit.ToString(), SqlDbType.VarChar).Value = user.Benefit;
                command.Parameters.Add(DBObjects.Users.multibank.ToString(), SqlDbType.Bit).Value = user.Multibank;
                command.Parameters.Add(DBObjects.Users.largoReferenciaUnivDelDev.ToString(), SqlDbType.Int).Value = user.LargoReferenciaUnivDelDev;
                command.Parameters.Add(DBObjects.Users.recordsDuration.ToString(), SqlDbType.VarChar).Value = user.RecordsDuration;
                command.Parameters.Add(DBObjects.Users.userId.ToString(), SqlDbType.Int).Value = user.UserId;

                DataConnection.Open(conn);

                command.ExecuteNonQuery();
            } 
        }

        private User ConvertToUser(IDataReader dr)
        {
            return new DataLookFor().MapToEntity<User>(dr, false);
        }

    }
}
